package org.noip.milhous.clause;

import java.util.LinkedList;
import java.util.List;

import org.noip.milhous.FieldName;
import org.noip.milhous.SQLInterface;

public class FieldLister
{
	List<Object> fields;

	StringBuffer cachedSQL = null;

	public FieldLister(Object... objects)
		{
			fields = new LinkedList<Object>();
			for (Object o : objects)
				{
					fields.add(o);
				}
		}

	public void addFields(Object... objs)
		{
			cachedSQL = null;
			for (Object o : objs)
				{
					fields.add(o);
				}
		}

	public String toSQL()
		{
			if (cachedSQL == null)
				{
					cachedSQL = new StringBuffer();
					String del = "";
					for (Object o : fields)
						{
							cachedSQL.append(del);
							del = ",";
							if (o instanceof SQLInterface)
								{
									cachedSQL.append(((SQLInterface) o).toSQL());
								}
							else if (o instanceof FieldName)
								{
									cachedSQL.append(" " + ((FieldName) o).getDatabaseFieldName() + " ");
								}
						}
				}
			return cachedSQL.toString();
		}
}
